package com.souku.finance.application.utils.level;

import android.content.Context;
import android.content.SharedPreferences;

/**
 * @author andong
 *         缓存类, 所有的数据都是采用SharedPreferences方式存储和获取.
 */
public class CacheUtils {

    private static final String CACHE_FILE_NAME = "com_xys_cache";

    private static SharedPreferences mSharedPreferences;

    /**
     * @param key      要取的数据的键
     * @param defValue 缺省值
     * @return
     */
    public static boolean getBoolean(String key, boolean defValue) {
        if (mSharedPreferences == null) {
            mSharedPreferences = UIUtils.getContext().getSharedPreferences(CACHE_FILE_NAME, Context.MODE_PRIVATE);
        }
        return mSharedPreferences.getBoolean(key, defValue);
    }

    /**
     * 存储一个boolean类型数据
     *
     * @param key
     * @param value
     */
    public static void putBoolean(String key, boolean value) {
        if (mSharedPreferences == null) {
            mSharedPreferences = UIUtils.getContext().getSharedPreferences(CACHE_FILE_NAME, Context.MODE_PRIVATE);
        }
        mSharedPreferences.edit().putBoolean(key, value).commit();
    }

    /**
     * 存储一个String类型的数据
     *
     * @param key
     * @param value
     */
    public static void putString(String key, String value) {
        if (mSharedPreferences == null) {
            mSharedPreferences = UIUtils.getContext().getSharedPreferences(CACHE_FILE_NAME, Context.MODE_PRIVATE);
        }
        mSharedPreferences.edit().putString(key, value).commit();
    }

    /**
     * 根据key取出一个String类型的值
     *
     * @param key
     * @param defValue
     * @return
     */
    public static String getString(String key, String defValue) {
        if (mSharedPreferences == null) {
            mSharedPreferences = UIUtils.getContext().getSharedPreferences(CACHE_FILE_NAME, Context.MODE_PRIVATE);
        }
        return mSharedPreferences.getString(key, defValue);
    }

    /**
     * 存储一个String类型的数据
     *
     * @param key
     * @param value
     */
    public static void putInt(String key, int value) {
        if (mSharedPreferences == null) {
            mSharedPreferences = UIUtils.getContext().getSharedPreferences(CACHE_FILE_NAME, Context.MODE_PRIVATE);
        }
        mSharedPreferences.edit().putInt(key, value).commit();
    }

    /**
     * 根据key取出一个String类型的值
     *
     * @param key
     * @param defValue
     * @return
     */
    public static int getInt(String key, int defValue) {
        if (mSharedPreferences == null) {
            mSharedPreferences = UIUtils.getContext().getSharedPreferences(CACHE_FILE_NAME, Context.MODE_PRIVATE);
        }
        return mSharedPreferences.getInt(key, defValue);
    }
}

